package com.easy.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.chainsaw.Main;

import com.easy.auth.domain.Report;


/**
 * 处理系统时间信息
 * TODO:目前不支持多线程，应考虑使用apache的相关实现
 * 
 * @author WenJian
 */
public abstract class DateUtil {

	private static String saturnDefaultDateFormat = "yyyy-MM-dd";

	private static String saturnDefaultTimeFormat = "yyyy-MM-dd HH:mm:ss";

	private static String defaultDateRegExpr = "[0-9]{4}\\-([01])?[0-9]\\-([0123])?[0-9](\\s*[0-9]{2}:[0-9]{2}:[0-9]{2})?";

	private static String dateFormat = saturnDefaultDateFormat;

	private static String timeFormat = saturnDefaultTimeFormat;

	private static String dateRegExpr = defaultDateRegExpr;

	
	
	public static String getSysteTimeMillisecond(Date date) {
		return new SimpleDateFormat("HH:mm:ss SSS").format(date);
	}
	
	/**
	 * 获得系统时间，默认格式yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getSystemTime() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(timeFormat);
		return format.format(date);
	}
 

	/**
	 * 获得系统日期，默认格式yyyy-MM-dd
	 * @return
	 */
	public static String getSystemDate() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		return format.format(date);
	}
	
	public static String getPath(){
		String DPV_PATH="";
		try {
			FileReader fr = new FileReader("C://FCC//path.txt");
			BufferedReader br = new BufferedReader(fr);
			DPV_PATH=br.readLine();
		} catch (Exception e) {
			System.out.println("file is not right!");
			return "";
			// TODO: handle exception
		}
		return DPV_PATH;
	} 
	/**
	 * 
	 * @return
	 */
	
	public static String getNewHtmlPathByPart(String filePath){
		//filePath="C:\\FCC\\HTMLReport\\5CA0_42410.pdf";
		File file;
		String part;
		try{
			file = new File(filePath);
			part = file.getName().substring(0,file.getName().lastIndexOf("."));
		}catch (Exception e) {
			System.out.println("getHtmlPathByPart file is not right!");
			return null;
		}
		List<File> fileList = getHtmlReportByPart(part);
		System.out.println("file totle :"+fileList.size());
		
		long max = 0;
		int maxNo = -1;
		for (int i = 0; i < fileList.size(); i++) {
			if(fileList.get(i).lastModified()>max){
				max = fileList.get(i).lastModified();
				maxNo =i;
			}
			//String file_create_time=DateUtil.getDateStringByformat("yyyy-MM-dd",fileList.get(i).lastModified());
		}
		if(maxNo>-1){
			String pa = fileList.get(maxNo).getName()+"\\"+part+"_Files\\File_1\\index.files\\gif_1.gif";
			System.out.println("fiel Path :"+pa.replace("\\", "/"));
			return pa.replace("\\", "/");
		}
		return null;
	}
	public static String getHtmlPathByPart(String filePath){
		//filePath="C:\\FCC\\HTMLReport\\5CA0_42410.pdf";
		File file;
		String part;
		try{
			file = new File(filePath);
			part = file.getName().substring(0,file.getName().lastIndexOf("."));
		}catch (Exception e) {
			System.out.println("getHtmlPathByPart file is not right!");
			return null;
		}
		List<File> fileList = getHtmlReportByPart(part);
		System.out.println("file totle :"+fileList.size());
		for (int i = 0; i < fileList.size(); i++) {
			System.out.println("time and time :"+(fileList.get(i).lastModified()-file.lastModified()));
			if(fileList.get(i).lastModified()-file.lastModified()<1800000&&fileList.get(i).lastModified()-file.lastModified()>-1800000){
				//String a = fileList.get(i).getAbsolutePath().lastIndexOf("")
				String pa = fileList.get(i).getName()+"\\"+part+"_Files\\File_1\\index.files\\gif_1.gif";
				System.out.println("fiel Path :"+pa.replace("\\", "/"));
				return pa.replace("\\", "/");
			}
			//String file_create_time=DateUtil.getDateStringByformat("yyyy-MM-dd",fileList.get(i).lastModified());
		}
		System.out.println("getHtmlPathByPart no compare file !");
		return null;
	}
	public static List<File> getHtmlReportByPart(String part){
		String DPV_PATH= Parameter.NAS_HTMLREPORT_PATH;
		int length = part.length();
		List<File> fileList = new ArrayList<File>();
		try {
			File directory = new File(DPV_PATH);
			File[] fileArray = directory.listFiles();
			for (int i = 0; i < fileArray.length; i++) {
				if (fileArray[i].isDirectory()) {
					String partName = fileArray[i].getName().substring(0,length);
					if(partName.equalsIgnoreCase(part)){
						fileList.add(fileArray[i]);
					}
				}
			}
			
		} catch (Exception e) {
			System.out.println("file is not right!");
			return fileList;
			// TODO: handle exception
		}
		return fileList;
	} 
	
	
	
	 
	
	/**
	 * 获得系统时间，根据给定格式
	 * @param formatStr
	 * @return
	 */
	public static String getSystemTime(String formatStr) {
		if (!isValidFormat(formatStr)) {
			formatStr = saturnDefaultTimeFormat;
		}

		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(date);
	}

	/**
	 * 获得系统日期，根据给定格式
	 * @param formatStr
	 * @return
	 */
	public static String getSystemDate(String formatStr) {
		if (!isValidFormat(formatStr)) {
			formatStr = saturnDefaultDateFormat;
		}
		
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(date);
	}
	
	/**
	 * 获得日期，根据给定格式
	 * @param formatStr
	 * @return
	 */
	public static String getDateStringByformat(String formatStr,Date date) {
		if (!isValidFormat(formatStr)) {
			formatStr = saturnDefaultTimeFormat;
		}
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(date);
	}
	
	/**
	 * 获得日期，根据给定格式
	 * @param formatStr
	 * @return
	 */
	public static String getDateStringByformat(String formatStr,long date) {
		Calendar cd = Calendar.getInstance();		
		cd.setTimeInMillis(date);
		if (!isValidFormat(formatStr)) {
			formatStr = saturnDefaultTimeFormat;
		}
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(cd.getTime());
	}
	
	/**
	 * 把字符串变成日期
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date getDate(String dateStr) {
		if (isValidDate(dateStr)) {
			SimpleDateFormat format = new SimpleDateFormat(dateFormat);
			Date date = null;
			try {
				date = format.parse(dateStr);
				return date;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}

	/**
	 * 把字符串变成时间
	 * @param dateStr
	 * @return
	 */
	public static Date getDateTime(String dateStr) {
		if (isValidDate(dateStr)) {
			SimpleDateFormat format = new SimpleDateFormat(timeFormat);
			Date date = null;
			try {
				date = format.parse(dateStr);
				return date;
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static boolean isValidFormat(String formatStr) {
		// TODO: 检查具体的时间日期的格式,目前只判断了是否为空
		if (formatStr != null) {
			return true;
		}
		return false;
	}

	public static boolean isValidDate(String dateStr) {
		if (dateStr != null) {
			if (dateStr.matches(dateRegExpr)) {
				if (checkDate(dateStr)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public static String getDateStr(String dateStr, String format) {
		Date date = DateUtil.getDateTime(dateStr);
		SimpleDateFormat simpleFormat = new SimpleDateFormat(format);
		return simpleFormat.format(date);
	}

	public static Date getDate(String dateStr, String format)
			throws ParseException {
		if (isValidDate(dateStr)) {
			SimpleDateFormat formatStr = new SimpleDateFormat(format);
			Date date = null;
			try {
				date = formatStr.parse(dateStr);
				return date;
			} catch (ParseException e) {
				throw e;
			}
		}
		return null;
	}

	public static boolean checkDate(String dateStr) {
		// TODO: 检查日期格式是否正确
		return true;
	}

	public static String getDateFormat() {
		return dateFormat;
	}

	public static void setDateFormat(String dateFormat) {
		DateUtil.dateFormat = dateFormat;
	}

	public static String getTimeFormat() {
		return timeFormat;
	}

	public static void setTimeFormat(String timeFormat) {
		DateUtil.timeFormat = timeFormat;
	}

	public static String getDateRegExpr() {
		return dateRegExpr;
	}

	public static void setDateRegExpr(String dateRegExpr) {
		DateUtil.dateRegExpr = dateRegExpr;
	}
	public static void main(String args[]){
		String filepath ="\\\\nas.fawcar.com\\nas_fs\\DPV\\Reports\\Plant1\\X80\\FARO_CMM\\Waixie\\FCC_Trigger\\FCC_Trigger_ckfykumjztsbdnajvwfrjs3l3y\\FCC_Trigger.pdf";
		int lastone=0;
		lastone = filepath.lastIndexOf('\\');
		String pdfname=filepath.substring(lastone+1);
		String filename=pdfname.replace(".pdf","");
		System.out.println(filename);
		int a=filepath.toLowerCase().indexOf(filename.toLowerCase());
		if(a==-1){
			//cuowu
			//return "";
		}
		String b=filepath.substring(a,filepath.lastIndexOf('\\')+1);
		String vsdpath=filepath.replace(b,"").replace(".pdf",".vsd");
		System.out.println(vsdpath);
		Map<String, String> map= new HashMap<String, String>();
		String dd=map.get("aa");
		if(dd==null||dd.equals("")){
			System.out.println(1231);
		}
	
	}
	
}
